package com.ruoyi.web.controller.zkcontroller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.web.domain.Dish;
import com.ruoyi.web.dto.DishDto;
import com.ruoyi.web.service.zkService.CategoryService;
import com.ruoyi.web.service.zkService.DishFlavorService;
import com.ruoyi.web.service.zkService.DishService;
import com.ruoyi.web.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/dish")
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private DishFlavorService dishFlavorService;
    @Autowired
    private CategoryService categoryService;

    /**
     * 菜品管理-新增菜品
     *
     * @param dishDto
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody DishDto dishDto) {
        log.info("新增菜品：", dishDto.getName());
        dishService.saveWithFlavor(dishDto);

        return R.ok("新增成功！");
    }

    /**
     * 菜品管理-分页查询菜品
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(@RequestParam("page") int page, @RequestParam("pageSize") int pageSize, @RequestParam("name") String name) {
        log.info("接收到菜品管理分页查询：当前页{},当前页条数{}", page, pageSize);
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(name != null, Dish::getName, name);

        wrapper.orderByDesc(Dish::getUpdateTime);
        //构造分页查询
        Page<Dish> dishPage = new Page<>(page, pageSize);
        Page<DishDto> dishDtoPage = new Page<>();
        //查询
        dishService.page(dishPage, wrapper);

        return R.ok(dishDtoPage);
    }

    /**
     * 菜品管理查询菜品信息和口味信息
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<DishDto> get(@PathVariable("id") Long id) {
        log.info("接收到查询菜品详细请求：菜品id{}", id);
        DishDto withFlavor = dishService.getWithFlavor(id);
        return R.ok(withFlavor);
    }
}
